package spring.beans.factory;

import spring.beans.config.SingletonBeanRegistry;

import java.util.concurrent.ConcurrentHashMap;

/**
 * 默认单例注册表
 */
public class DefaultSingletonBeanRegistry implements SingletonBeanRegistry {

    /**
     * 单例池，一级缓存
     */
    private ConcurrentHashMap<String, Object> singletonObjects = new ConcurrentHashMap<>();



    @Override
    public Object getSingleton(String beanName) {
        return singletonObjects.get(beanName);
    }

    @Override
    public void registerSingleton(String beanName, Object singletonObject) {
        if (containsSingleton(beanName)){
            System.out.println("===重复注册单例"+beanName+"，已覆盖===");
        }
        singletonObjects.put(beanName, singletonObject);
    }

    @Override
    public boolean containsSingleton(String beanName) {
        return singletonObjects.containsKey(beanName);
    }
}
